package com.tensquare.article.service;

import com.tensquare.article.pojo.Comment;
import com.tensquare.article.repository.CommentRepository;
import com.tensquare.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class CommentService {
    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MongoTemplate mongoTemplate;

    public List<Comment> findAll() {
        List<Comment> list = commentRepository.findAll();
        return list;
    }

    public Comment findById(String commentId) {
        /*Optional<Comment> optional=commentRepository.findById(commentId);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;*/
        Comment comment = commentRepository.findById(commentId).get();
        return comment;
    }

    public void save(Comment comment) {
        //分布式id生成器生成id
        String id=idWorker.nextId()+"";
        comment.set_id(id);
        //初始化点赞数据，发布时间等
        comment.setThumbup(0);
        comment.setPublishdate(new Date());
        //保存数据
        commentRepository.save(comment);
    }

    public void updateById(Comment comment) {
        //使用mongoRepository的方法
        //其中save方法，主键如果存在执行修改，主键不存在执行新增
        commentRepository.save(comment);
    }

    public void deleteById(String commentId) {
        commentRepository.deleteById(commentId);
    }

    public List<Comment> findByArticleId(String articleId) {
        //调用持久层，根据文章id查询
        List<Comment> list = commentRepository.findByArticleid(articleId);
        return list;
    }

    public void thumbup(String commentId){
        ////查询评论
        //Comment comment=commentRepository.findById(commentId).get();
        ////修改点赞数
        //comment.setThumbup(comment.getThumbup()+1);
        //commentRepository.save(comment);

        //点赞功能优化
        //直接修改参数
        //第一个参数是修改的条件
        //第二个参数是修改的数值
        //第三个参数是MongoDB的集合名称
        //封装修改的条件
        Query query=new Query();
        query.addCriteria(Criteria.where("_id").is(commentId));
        //封装修改的数值
        Update update=new Update();
        update.inc("thumbup",1);

        mongoTemplate.updateFirst(query,update,"comment");
    }
}
